package com.xxxy.jsq.action.user;

import com.alibaba.fastjson2.JSONObject;
import com.xxxy.jsq.dao.UserDao;
import com.xxxy.jsq.model.Page;
import com.xxxy.jsq.model.User;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "getAllUserPageServlet", value = "/getAllUserServlet")  // 修改映射路径
public class getAllUserPageServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");  // 修改为 application/json


        User model = new User();
        UserDao dao = new UserDao();
        String userName = request.getParameter("userName");
        String userFlag = request.getParameter("userFlag");
        String teacherId = request.getParameter("teacherId");
        String roleId = request.getParameter("roleId");

        if (userName != null && !userName.equals("")) {
            model.setUserName(userName);
        }
        if (userFlag != null && !userFlag.equals("")) {
            model.setUserFlag(userFlag);
        }
        if (teacherId != null && !teacherId.equals("")) {
            model.setTeacherId(Long.parseLong(teacherId));
        }
        if (roleId != null && !roleId.equals("")) {
            model.setRoleId(Long.parseLong(roleId));
        }

        String cpage = request.getParameter("page");
        String limit = request.getParameter("limit");
        Page page = new Page();
        if (cpage != null && !cpage.equals("")) {
            page.setCurrentPage(Integer.parseInt(cpage));
            page.setCount(Integer.parseInt(limit));
        }
        List<User> list = dao.getAllDataByPage(model, page);
        int total = dao.getCount(model);

        JSONObject jo = new JSONObject();
        jo.put("code", 0);
        jo.put("msg", "");
        jo.put("count", total);
        jo.put("data", list);

        PrintWriter out = response.getWriter();
        out.print(jo.toJSONString());
        out.flush();
        out.close();
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
